Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

115
Visualizações
"Page Not Found/404 handler” to Swift Express Server

I’m writing the simplest server via Swift Express

And I want to add custom handler for “url not found” so that if user write “/notFoundUrl” he will see something like that: “Url “notFoundUrl” not found, please go to home page”.

I’ve added:

app.get("/:notFoundUrl+") { (request:Request<AnyContent>)->Action<AnyContent> in
   print(request.params["notFoundUrl"])
   return Action<AnyContent>.render("index", context: ["hello": "Page Not Found: " + request.params["notFoundUrl"]!])
}

But it’s not appropriate becouse:

  • Order does matter.
  • I can’t return 404 error.

So how to add custom “Page Not Found/404 handler” to Swift Express Server?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It much simpler than you are trying to do it.

/// Custom page not found error handler
app.errorHandler.register { (e:ExpressError) in
    switch e {
    case .PageNotFound(let path):
        return Action<AnyContent>.render("404", context: ["path": path], status: .NotFound)
    default:
        return nil
    }
}

The full do can be found here: https://github.com/crossroadlabs/Express/blob/master/doc/gettingstarted/errorhandling.md

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda